// package/pages/cardDetail/cardDetail.js
let wxbarcode = require('../../../utils/wxbarcode/index.js')
let myRequest = require('../../../common/script/myRequest.js');
let api = require('../../../common/script/api.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    hiddenLoading: true,
    memberData: {},
    code: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    that.getCardinfoData();
    // this.aginGetdata();
    this.timeOut = null;

  },

  //解绑
  btn() {
    const that = this;
    wx.showModal({
      content: '是否解绑会员卡?',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          let data = {}
          wx.showLoading({
            title: '加载中...',
          })
          myRequest.EasyHttp.post(api.reqUrls.unbindmember, data).then(res => {
            wx.hideLoading();
            if (res.data.status == 1) {
              wx.setStorageSync('isVip', false)
              wx.setStorageSync('refresh', false)
              wx.setStorageSync('memberData', null)
              wx.setStorageSync('isfoundingmember', false)
              wx.setStorageSync('cardnumber', '')
              wx.setStorageSync('totalamount', 0)
              wx.showModal({
                content: res.data.msg,
                showCancel: false,
                success() {
                  wx.navigateBack()
                }
              })
            } else {
              wx.showModal({
                content: res.data.msg,
                showCancel: false,
                confirmText: '我知道了',
                confirmColor: '#ff5151',
                success(e) {}
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  getCardinfoData() {
    const that = this;
    let data = {}
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.membercardShow, data).then(res => {

      if (res.data.status == 1) {
        that.setData({
          memberData: res.data.result.member_data,
        })
        that.aginGetdata()
      } else {
        wx.hideLoading();
        wx.showModal({
          content: res.data.msg,
          showCancel: false,
          confirmText: '我知道了',
          confirmColor: '#ff5151',
          success(e) {
            wx.navigateBack()
          }
        })
      }
    })

  },

  aginGetdata(e) {
    const that = this;
    let data = {}
    myRequest.EasyHttp.post(api.reqUrls.getCardNumberCode, data).then(res => {
      if (res.data.status == 1) {
        that.setData({
          code: res.data.result.card_number_code
        })
        setTimeout(() => {
          wx.hideLoading();
          that.qrCode(res.data.result.card_number_code)
        }, 500)
      } else {
        wx.hideLoading();
      }
    })
  },

  qrCode(code) {
    const query = this.createSelectorQuery()
    query.select('#qrcode').fields({
      node: true,
      size: true
    }).exec(res => {
      const canvas = res[0].node
      wxbarcode.wxqrcode({
        canvas: canvas,
        canvasId: 'qrcode',
        width: 200,
        padding: 50,
        paddingColor: '#fff',
        background: '#fff',
        foreground: '#000000',
        text: code,
      });
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})